package cn.gyx.mq.ES;

import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;

public class es {
    private TransportClient getClient() throws Exception {
        Settings setting = Settings.builder()
                .put("client.transport.sniff", true).build();
        TransportClient client = null;
        try {
            client = new PreBuiltTransportClient(setting)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return client;
    }

    @Test
    public void add() throws Exception {
        for (int i = 1; i <= 5; i++) {
            // 获取链接
            TransportClient client = getClient();
            // 创建索引
            IndexRequestBuilder index = client.prepareIndex("pethome", "user", i+"");
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", i+"");
            map.put("name", i%2==0?"tom" :"jack");
            map.put("sex", i%2==0?1:0);
            map.put("age", i+"0");
            IndexResponse indexResponse = index.setSource(map).get();//获取结果
            System.out.println(indexResponse);
//            System.out.println(666);
        }
    }

    @Test
    private void get() throws Exception {
        TransportClient client = getClient();

        GetRequestBuilder getRequestBuilder = client.prepareGet("pethome", "user", "1");

        GetResponse response = getRequestBuilder.get();
        System.out.println(response);
    }

    @Test
    private void update() throws Exception {
        TransportClient client = getClient();

        UpdateRequestBuilder update = client.prepareUpdate("pethome", "user", "1");

        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "jerry");

        UpdateResponse response = update.setDoc(map).get();
        System.out.println(response);
    }

    @Test
    private void delete() throws Exception {
        TransportClient client = getClient();

        DeleteRequestBuilder delete = client.prepareDelete("pethome", "user", "1");

        DeleteResponse response = delete.get();
        System.out.println(response);
    }

    @Test
    private void search() throws Exception {
        TransportClient client = getClient();

        SearchRequestBuilder pethome = client.prepareSearch("pethome");

        // 排序
        pethome.addSort("age", SortOrder.DESC);
        //分页
        pethome.setFrom(0);
        pethome.setSize(20);
        //搜索条件
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        boolQuery.must(QueryBuilders.matchQuery("name", "tome jack"));
        boolQuery.filter(QueryBuilders.termQuery("sex", 0))
                .filter(QueryBuilders.rangeQuery("age").gte(10).lte(30));

        SearchResponse response = pethome.setQuery(boolQuery).get();

        // 查询结果
        SearchHits hits = response.getHits();
        // 查询总条数
        long totalHits = hits.getTotalHits();
        System.out.println(totalHits);
        SearchHit[] searchHits = hits.getHits();
        for (SearchHit searchHit : searchHits) {
            Map<String, Object> source = searchHit.getSource(); // 返回一个map
            System.out.println(source);
        }
    }

}
